<template>
  <div class="app-container">
    <div class="filter-container" style="margin-bottom:20px;float:right">
      <el-button type="primary"  @click="handleCreate">新增科目</el-button>
    </div>
    <el-table :data="list" v-loading.body="listLoading" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label='科目ID' min-width="110">
        <template slot-scope="scope">
          {{scope.row.id}}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="科目名称" min-width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.subjectName }}
        </template>
      </el-table-column>
      <el-table-column label="排序" min-width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.sort }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="110">
        <template slot-scope="scope">
          <el-button size="small" @click="handleUpdate(scope.row)">编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="dialogStatus=='create' ? '新增科目' : '编辑科目'" :visible.sync="dialogFormVisible" center>
      <el-form :model="form" :rules="rules" ref="ruleForm">
        <el-form-item label="科目名称" label-width="80px" prop="subjectName">
          <el-input v-model="form.subjectName" auto-complete="off" maxlength="5"
          placeholder="请输入科目名称（不超过5个字）"></el-input>
        </el-form-item>
        <el-form-item label="排序" label-width="80px" prop="sort">
          <el-input-number  v-model="form.sort" :step="1" :min="0"></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="addForm">确 定</el-button>
        <el-button v-else type="primary" @click="updateForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { getSubjectList, add, update } from '@/api/subject'

export default {
  data() {
    return {
      dialogStatus:'create',
      form:{
        id:0,
        subjectName:'',
        sort:1
      },
      dialogFormVisible:false,
      list: null,
      listLoading: true,
      rules: {
        subjectName: [{ required: true, message: '请输入科目名称', trigger: 'blur' }],
        sort: [{ required: true, message: '请输入排序权重', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    handleCreate() {
      const { list } = this
      if(list.length >= 20){
        this.$message('最多配置20个科目')
        return
      }
      this.form = {
        id:0,
        subjectName:'',
        sort:1
      }
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    handleUpdate(row) {
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.form = Object.assign({}, row)
      })
    },
    fetchData() {
      this.listLoading = true
      getSubjectList().then(response => {
        this.list = response.data
        this.listLoading = false
      })
    },
    updateForm(){
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          const { form } = this
          update(form).then(response => {
            const { code, message } = response
            if(code == 0){
              this.fetchData()
              this.dialogFormVisible = false
            }else{
              this.$message(message)
            }
          })
        } else {
          return false
        }
      })
    },
    addForm() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          const { form } = this
          add(form).then(response => {
            const { code, message } = response
            if(code == 0){
              this.fetchData()
              this.dialogFormVisible = false
            }else{
              this.$message(message)
            }
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>
